<!-- 新增编辑员工 -->
<template>
  <div>
    <el-dialog title="修改密码" :visible.sync="editVisible">
      <el-form
        class="formView"
        ref="form"
        :model="formdata"
        :rules="rules"
        label-position="right"
        label-width="100px"
      >
        <el-form-item label="新密码:" prop="password">
          <el-input
            v-model.trim="formdata.password"
            clearable
            type="password"
            placeholder="新密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码:" prop="confimPass">
          <el-input
            v-model.trim="formdata.confimPass"
            clearable
            type="password"
            placeholder="请再次输入密码"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { resetPassword } from '@/api/rightsManage'
export default {
  components: {},
  props: {
    detailData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      editVisible: false,
      formdata: {
        password: '',
        confimPass: ''
      },
      rules: {
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, message: '请输入大于6位密码', trigger: 'blur' }
        ],
        confimPass: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { min: 6, message: '请输入大于6位密码', trigger: 'blur' }
        ]
      }
    }
  },
  created() {},
  mounted() {},
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.formdata.password !== this.formdata.confimPass) {
            this.$message({
              message: '两次密码不正确，请重新输入',
              type: 'error'
            })
          } else {
            this.onSubmit()
          }
        } else {
          return false
        }
      })
    },
    onSubmit() {
      resetPassword(
        Object.assign({}, this.formdata, { userById: this.detailData.userId })
      ).then(res => {
        this.$message({
          message: '密码重置成功',
          type: 'success'
        })
        this.$parent.getDataList()
        this.resetForm('form')
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.formdata = {
        password: '',
        confimPass: ''
      }
      this.editVisible = false
    }
  },
  watch: {
    editVisible: {
      deep: true,
      handler(now, old) {
        if (!now) {
          this.resetForm('form')
        }
      }
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep {
  .el-dialog {
    width: 500px;
    border-radius: 10px;
  }
  .el-dialog__title {
    font-size: 16px;
  }
  .el-input {
    width: 250px;
  }
  .el-dialog__body {
    padding-top: 0;
    padding-bottom: 0px;
  }
  .el-form {
    padding: 10px 0;
  }
  .el-input__inner {
    height: 35px;
    line-height: 35px;
  }
  .el-form-item {
    margin-bottom: 15px;
    &:last-child {
      margin-bottom: 0;
    }
  }
  .el-dialog__footer {
    padding-top: 10px;
    padding-bottom: 10px;
    border-top: 1px solid #eee;
  }
}
.formView {
  border-top: 1px solid #eee;
  .sortInput {
    width: 100px;
  }
}
</style>
